﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using Microsoft.Xrm.Sdk.Metadata;

namespace MetadataDocumentationGenerator.Generator
{
    public class Document2Base
    {

        protected IEnumerable<AttributeMetadata> GetSelectedAttributes(GenerationSettings settings, XmlDocument doc, EntityMetadata emd)
        {
            IEnumerable<AttributeMetadata> amds = null;

            switch (settings.AttributesSelection)
            {
                case AttributeSelectionOption.AllAttributes:
                    amds = emd.Attributes;
                    break;
                case AttributeSelectionOption.AttributesOptionSet:
                    amds =
                        emd.Attributes.Where(x => x.AttributeType != null && (x.AttributeType.Value == AttributeTypeCode.Boolean
                                                                              ||
                                                                              x.AttributeType.Value ==
                                                                              AttributeTypeCode.Picklist
                                                                              ||
                                                                              x.AttributeType.Value == AttributeTypeCode.State
                                                                              ||
                                                                              x.AttributeType.Value == AttributeTypeCode.Status));
                    break;
                case AttributeSelectionOption.AttributeManualySelected:
                    amds =
                        emd.Attributes.Where(
                            x =>
                            settings.EntitiesToProceed.First(y => y.Name == emd.LogicalName).Attributes.Contains(
                                x.LogicalName));
                    break;
                case AttributeSelectionOption.AttributesOnForm:
                    amds =
                        emd.Attributes.Where(
                            x =>
                            doc.SelectSingleNode("//control[@datafieldname='" + x.LogicalName + "']") != null);
                    break;
            }
            return amds;
        }
    }
}
